﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EdgeStatz.ModelEngine.FormTools;
using EdgeStatz.ModelEngine.FolderTools;
using EdgeStatz.ModelEngine.DatabaseTools;
using EdgeStatz.ESSystems.Objects;

namespace EdgeStatz.DesignTools
{
    public partial class Designer : Form
    {
        #region Class Variables
        private static FormEngine formEngine;
        private static FolderEngine folderEngine;
        private static DBStructureEngine databaseEngine;
        
        public static FormEngine FormEngine
        {
            get { return Designer.formEngine; }
            set { Designer.formEngine = value; }
        }
        public static FolderEngine FolderEngine
        {
            get { return Designer.folderEngine; }
            set { Designer.folderEngine = value; }
        }
        public static DBStructureEngine DatabaseEngine
        {
            get { return Designer.databaseEngine; }
            set { Designer.databaseEngine = value; }
        }
        #endregion

        FolderDesigner folderDesigner;
        DatabaseDesigner databaseDesigner;
        FormGenDesigner formGenDesigner;

        public Designer()
        {
            InitializeComponent();
            GlobalRegistrySettings.Load();
        }
        
        private void tsmiFile_Open_Click(object sender, EventArgs e)
        {
            if (GlobalSystemSettings.ModelFilePath != null)
                tsmiFile_Close_Click(null, e);

            OpenFileDialog ofdialog = new OpenFileDialog();
            ofdialog.Title = "Open Model File";
            ofdialog.Filter = "EdgeStatz Model|*.esm|XML|*.xml|All files|*.*";
            ofdialog.Multiselect = false;
            if (ofdialog.ShowDialog() == DialogResult.OK)
            {
                LoadModelFile(ofdialog.FileName);
            }
        }
        private void LoadModelFile(string filename)
        {
            GlobalSystemSettings.ModelFilePath = filename;

            databaseEngine = new DBStructureEngine(GlobalSystemSettings.ModelFilePath);

            GlobalSystemSettings.DatabaseName = databaseEngine.DatabaseName;
            GlobalSystemSettings.ModelName = databaseEngine.ModelName;
            GlobalSystemSettings.SystemFolderPath = databaseEngine.ModelPath;
            GlobalObjectsSQL.SetConnectionString(databaseEngine.GetDefaultProvider());

            tbModel_Name.Text = databaseEngine.ModelName;
            tbModel_SysPath.Text = databaseEngine.ModelPath;

            formEngine = new FormEngine(GlobalSystemSettings.ModelFilePath);
            folderEngine = new FolderEngine(GlobalSystemSettings.ModelFilePath);

            this.llStage_1.Visible = true;
            this.llStage_2.Visible = true;
            this.llStage_3.Visible = true;
            this.llStage_4.Visible = true;

            OpenDesigners();
            llStage_1_LinkClicked(null, null);           
        }

        private void OpenDesigners()
        {
            this.folderDesigner = new EdgeStatz.DesignTools.FolderDesigner();

            this.folderDesigner.Dock = System.Windows.Forms.DockStyle.Fill;
            this.folderDesigner.Location = new System.Drawing.Point(0, 48);
            this.folderDesigner.Name = "folderDesigner";
            this.folderDesigner.Size = new System.Drawing.Size(550, 463);
            this.folderDesigner.TabIndex = 0;

            this.databaseDesigner = new EdgeStatz.DesignTools.DatabaseDesigner();

            this.databaseDesigner.Dock = System.Windows.Forms.DockStyle.Fill;
            this.databaseDesigner.Location = new System.Drawing.Point(0, 48);
            this.databaseDesigner.Name = "databaseDesigner";
            this.databaseDesigner.Size = new System.Drawing.Size(550, 463);
            this.databaseDesigner.TabIndex = 0;

            this.formGenDesigner = new EdgeStatz.DesignTools.FormGenDesigner();

            this.formGenDesigner.Dock = System.Windows.Forms.DockStyle.Fill;
            this.formGenDesigner.Location = new System.Drawing.Point(0, 48);
            this.formGenDesigner.Name = "formGenDesigner";
            this.formGenDesigner.Size = new System.Drawing.Size(550, 463);
            this.formGenDesigner.TabIndex = 0;
        }

        private void tsmiFile_New_Click(object sender, EventArgs e)
        {
            tsmiFile_Close_Click(null, null);

            this.llStage_1.Visible = true;
            llStage_1_LinkClicked(null, null);

            // Show Model info textboxes & instructions/help
        }
        
        private void tsmiFile_Save_Click(object sender, EventArgs e)
        {
            if (GlobalSystemSettings.ModelFilePath == null || GlobalSystemSettings.ModelFilePath == "")
            {
                if (tbModel_Name.Text.Trim().Equals(""))
                    MessageBox.Show("Missing Value: \nModel name cannot be empty.", "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                if (tbModel_SysPath.Text.Trim().Equals(""))
                    MessageBox.Show("Missing Value: \nSystem path cannot be empty.", "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

                SaveFileDialog sfdialog = new SaveFileDialog();
                sfdialog.Title = "Create New Model File";
                sfdialog.Filter = "EdgeStatz Model|*.esm|XML|*.xml|All files|*.*";
                sfdialog.DefaultExt = ".esm";
                if (sfdialog.ShowDialog() == DialogResult.OK)
                {
                    XmlModelFile.CreateFile(sfdialog.FileName, tbModel_Name.Text, tbModel_SysPath.Text, false);

                    LoadModelFile(sfdialog.FileName);
                }
            }
            else
            {
                //update model name and system path
                databaseDesigner.Save();
                folderDesigner.Save();
                formGenDesigner.Save();
            }
        }

        private void tsmiFile_SaveAs_Click(object sender, EventArgs e)
        {

        }
        private void llStage_1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.splitContainer1.Panel2.Controls.Clear();
            this.splitContainer1.Panel2.Controls.Add(this.panelModelInfo);
        }

        private void llStage_2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (databaseDesigner == null)
                return;
            this.splitContainer1.Panel2.Controls.Clear();
            this.splitContainer1.Panel2.Controls.Add(databaseDesigner);
        }

        private void llStage_3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (folderDesigner == null)
                return;
            this.splitContainer1.Panel2.Controls.Clear();
            this.splitContainer1.Panel2.Controls.Add(folderDesigner);
        }

        private void llStage_4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (formGenDesigner == null)
                return;
            this.splitContainer1.Panel2.Controls.Clear();
            this.splitContainer1.Panel2.Controls.Add(formGenDesigner);
        }

        private void bSysPath_Browse_Click(object sender, System.EventArgs e)
        {
            FolderBrowserDialog fbdialog = new FolderBrowserDialog();
            fbdialog.ShowNewFolderButton = true;
            fbdialog.Description = "System Folder Path";
            if (fbdialog.ShowDialog() == DialogResult.OK)
            {
                tbModel_SysPath.Text = fbdialog.SelectedPath;
            }
        }
        private void tsmiFile_Close_Click(object sender, EventArgs e)
        {
            if (GlobalSystemSettings.ModelFilePath != null && GlobalSystemSettings.ModelFilePath != "")
            {
                switch (MessageBox.Show(
                    "Do you want to save any changes made since the last save?",
                    "Save File Changes", MessageBoxButtons.YesNoCancel))
                {
                    case DialogResult.Yes:
                        tsmiFile_Save_Click(null, null);
                        break;
                    case DialogResult.No:
                        break;
                    case DialogResult.Cancel:
                        return;
                }
            }
            this.llStage_1.Visible = false;
            this.llStage_2.Visible = false;
            this.llStage_3.Visible = false;
            this.llStage_4.Visible = false;

            folderDesigner = null;
            databaseDesigner = null;
            formGenDesigner = null;

            formEngine = null;
            folderEngine = null;
            databaseEngine = null;

            GlobalSystemSettings.ModelFilePath = null;
            GlobalSystemSettings.DatabaseName = "master";
            GlobalSystemSettings.ModelName = null;
            GlobalSystemSettings.SystemFolderPath = null;
            GlobalObjectsSQL.SetConnectionString("localhost", GlobalSystemSettings.DatabaseName);

            this.splitContainer1.Panel2.Controls.Clear();
        }
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void compileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (formEngine == null)
            { MessageBox.Show("Model file must be selected"); return; }
            formEngine.CompileNewAssembly();
            MessageBox.Show("Assembly compiled");
        }
    }
}
